<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>mapper</title>
    <div th:replace="modules/header::common_resource"></div>
</head>
<body>
<div class="auto">
    <h1 style="text-align: center">方法生成</h1>
    <form id="autoForm" action="#" th:action="@{auto}" method="post">
        <div class="checkbox">
            <label >请选择要生成的文件:</label>
            <label>
                <input name="cfileName" value="Controller"
                       type="checkbox">controller
            </label>
            <label>
                <input name="cfileName" value="Service"  type="checkbox">service
            </label>
            <label>
                <input name="cfileName" value="ServiceImpl"
                       type="checkbox">serviceImpl
            </label>
            <label>
                <input name="cfileName" value="Mapper"  type="checkbox">mapper
            </label>
            <!--<label>
                <input name="cfileName" value="Mapper.xml" type="checkbox">Mapper.xml
            </label>-->
        </div>
        <div class="form-group">
            <input type="text" name="filename" class="form-control" id="filename"
                   placeholder="请输入文件前缀">
        </div>
        <div class="form-group">
            <input type="text" name="author" class="form-control" id="author"
                   value = "jiwd"
                   placeholder="请输入创建人">
        </div>
        <div class="form-group">
            <input type="text" name="description" class="form-control" id="description"
                   placeholder="请输入方法描述">
        </div>
        <div class="form-group">
            <input type="text" name="fun" class="form-control" id="fun"
                   placeholder="请输入方法名">
        </div>
        <div class="form-group">
            <input type="text" name="fhlx" class="form-control" id="fhlx"
                   value="void"
                   placeholder="请输入返回类型:如返回String或返回实体List<DataTest> 等">
        </div>
        <div class="form-group">
            <input type="text" name="cslx" class="form-control" id="cslx"
                   placeholder="请输入参数类型:如String a,Integer b等">
        </div>
    </form>
    <div class="auto">
    <button type="button" class="btn btn-success" id="smt">Submit</button>
    </div>
</div>
</body>
<script type="text/javascript">
    //扩展jquery的格式化方法
    $.fn.parseForm=function(){
        var serializeObj={};
        var array=this.serializeArray();
        var str=this.serialize();
        $(array).each(function(){
            if(serializeObj[this.name]){
                if($.isArray(serializeObj[this.name])){
                    serializeObj[this.name].push(this.value);
                }else{
                    serializeObj[this.name]=[serializeObj[this.name],this.value];
                }
            }else{
                serializeObj[this.name]=this.value;
            }
        });
        return serializeObj;
    };
    $(document).ready(function(){
        $("#smt").click(function(){
            var param = $("#autoForm").parseForm();
            param.date = new Date();
            var cfn = param.cfileName;
            if(cfn === undefined){
                alert("请选择要生成的文件");
                return false;
            }
            var filename = param.filename;
            if(null == filename || "" == filename){
                alert("请输入文件前缀");
                return false;
            }
            param.name = filename;
            var szm = filename.substring(0,1).toLowerCase();
            var hq = filename.substring(1,filename.length);
            param.filename = szm+hq;
            if(null == param.fun || "" == param.fun){
                alert("请输入方法名");
                return false;
            }
            if(null == param.fhlx || "" == param.fhlx){
                param.fhlx = "void";
            }
            $('input[name="cfileName"]:checked').each(function(){
                var vl = $(this).val();
                var tmp = template(vl, param);
                // param.vl=tmp;
                if(vl == "Controller"){
                    param.Controller = tmp
                }else if(vl == "Service"){
                    param.Service = tmp
                }else if(vl == "ServiceImpl"){
                    param.ServiceImpl = tmp
                }else if(vl == "Mapper"){
                    param.Mapper = tmp
                }
                // alert(tmp);
            });
            $.post("auto",{map:param},function(result){
                alert("提交成功");
            });
            // var temp = template('serviceImpl', param);
            // $("#autoForm").submit();
        });
    });
</script>
<script id="Controller" type="text/html">

    /**
    * {{description}}
    * @author {{author}}
    * @date {{dateFormat(date,'yyyy-MM-dd hh:mm:ss')}}
    * @param {{#cslx}}
    * @return {{#fhlx}}
    */
    @RequestMapping
    public {{#fhlx}} {{fun}}({{#cslx}}){
    {{if fhlx == 'void' }}
        {{filename}}Service.{{fun}}({{#cslx}});
    {{else}}
        return {{filename}}Service.{{fun}}({{#cslx}});
    {{/if}}
    }
}
</script>
<script id="Service" type="text/html">

     /**
     * {{description}}
     * @author {{author}}
     * @date {{dateFormat(date,'yyyy-MM-dd hh:mm:ss')}}
     * @param {{#cslx}}
     * @return {{#fhlx}}
     */
    {{#fhlx}} {{fun}}({{#cslx}});
}
</script>
<script id="ServiceImpl" type="text/html">

    /**
    * {{description}}
    * @author {{author}}
    * @date {{dateFormat(date,'yyyy-MM-dd hh:mm:ss')}}
    * @param {{#cslx}}
    * @return {{#fhlx}}
    */
    @Override
    public {{#fhlx}} {{fun}}({{#cslx}}){
    {{if fhlx == 'void' }}
        {{filename}}Mapper.{{fun}}({{#cslx}});
    {{else}}
        return {{filename}}Mapper.{{fun}}({{#cslx}});
    {{/if}}
    }
}
</script>
<script id="Mapper" type="text/html">

    /**
    * {{description}}
    * @author {{author}}
    * @date {{dateFormat(date,'yyyy-MM-dd hh:mm:ss')}}
    * @param {{#cslx}}
    * @return {{#fhlx}}
    */
    {{#fhlx}} {{fun}}({{#cslx}});
}
</script>
<script id="mapperx" type="text/html">
   <select resultType="{{}}" parameterType="{{}}">

   </select>
</script>
<style type="text/css">
    .auto{
        width:1024px;
        margin: 20px auto;
    }
    .auto input,button,label{
        margin: 5px;
    }
</style>
</html>